package net.mcreator.compresstools.procedures;

import net.minecraft.world.server.ServerWorld;
import net.minecraft.world.gen.feature.template.Template;
import net.minecraft.world.gen.feature.template.PlacementSettings;
import net.minecraft.world.gen.Heightmap;
import net.minecraft.world.World;
import net.minecraft.world.IWorld;
import net.minecraft.world.Explosion;
import net.minecraft.util.math.RayTraceContext;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.Rotation;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.Mirror;
import net.minecraft.entity.Entity;

import net.mcreator.compresstools.CompressToolsMod;

import java.util.Map;

public class CompressDiamondSwordDangYouJianDianJiKongQiShiProcedure {

	public static void executeProcedure(Map<String, Object> dependencies) {
		if (dependencies.get("world") == null) {
			if (!dependencies.containsKey("world"))
				CompressToolsMod.LOGGER.warn("Failed to load dependency world for procedure CompressDiamondSwordDangYouJianDianJiKongQiShi!");
			return;
		}
		if (dependencies.get("entity") == null) {
			if (!dependencies.containsKey("entity"))
				CompressToolsMod.LOGGER.warn("Failed to load dependency entity for procedure CompressDiamondSwordDangYouJianDianJiKongQiShi!");
			return;
		}
		IWorld world = (IWorld) dependencies.get("world");
		Entity entity = (Entity) dependencies.get("entity");
		double nx = 0;
		double nz = 0;
		double ny = 0;
		nx = (entity.world.rayTraceBlocks(new RayTraceContext(entity.getEyePosition(1f),
				entity.getEyePosition(1f).add(entity.getLook(1f).x * 20, entity.getLook(1f).y * 20, entity.getLook(1f).z * 20),
				RayTraceContext.BlockMode.OUTLINE, RayTraceContext.FluidMode.NONE, entity)).getPos().getX());
		nz = (entity.world.rayTraceBlocks(new RayTraceContext(entity.getEyePosition(1f),
				entity.getEyePosition(1f).add(entity.getLook(1f).x * 20, entity.getLook(1f).y * 20, entity.getLook(1f).z * 20),
				RayTraceContext.BlockMode.OUTLINE, RayTraceContext.FluidMode.NONE, entity)).getPos().getZ());
		ny = (world.getHeight(Heightmap.Type.MOTION_BLOCKING_NO_LEAVES, (int) nx, (int) nz));
		if (world instanceof World && !((World) world).isRemote) {
			((World) world).createExplosion(null, (int) nx, (int) ny, (int) nz, (float) 6, Explosion.Mode.DESTROY);
		}
		if (world instanceof ServerWorld) {
			Template template = ((ServerWorld) world).getStructureTemplateManager()
					.getTemplateDefaulted(new ResourceLocation("compress_tools", "stone"));
			if (template != null) {
				template.func_237144_a_((ServerWorld) world, new BlockPos((int) (nx - 2), (int) (ny - 3), (int) (nz - 2)),
						new PlacementSettings().setRotation(Rotation.NONE).setMirror(Mirror.NONE).setChunk(null).setIgnoreEntities(false),
						((World) world).rand);
			}
		}
	}
}
